package com.IC.base.service.impl;

import IC.common.core.map.VO.AllCountVO;

import IC.common.core.map.VO.HouseTypeVO;
import IC.common.core.map.VO.PopulationCount;
import com.IC.base.mapper.PlotMapper;
import com.IC.base.service.PlotService;
import com.IC.domain.dto.PlotDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.IC.domain.po.PlotPO;
import com.IC.domain.vo.PlotVO;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

@Service
@Log4j2
public class PlotServieImpl extends ServiceImpl<PlotMapper, PlotPO> implements PlotService{

    @Autowired
    private PlotMapper plotMapper;

    @Override
    public PageInfo<PlotVO> findlist(PlotDTO plotDTO) {
        PageHelper.startPage(plotDTO.getPageNum(),plotDTO.getPageSize());
        List<PlotVO> findlist = plotMapper.findlist(plotDTO);
        PageInfo<PlotVO> pageInfo = new PageInfo<>(findlist);
        return pageInfo;
    }

    @Override
    public PageInfo<PlotVO> findAll(Integer pageNum, Integer pageSize) {
        // 使用PageHelper进行分页查询的初始化
        PageHelper.startPage(pageNum, pageSize);
        List<PlotVO> all = plotMapper.findList();
        // 遍历所有区域数据，为每个区域异步统计人员、车辆、设备和房屋信息
        for (PlotVO plotVO : all) {
            // 异步统计人员和车辆信息
            CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
                PlotVO byPlotId = plotMapper.findCarByPlotIdCar(plotVO.getId());
                plotVO.setPopulationCount(byPlotId.getPopulationCount());
                plotVO.setForeignCount(byPlotId.getForeignCount());
                plotVO.setForeignerCount(byPlotId.getForeignerCount());
                plotVO.setRegisterCount(byPlotId.getRegisterCount());
                plotVO.setCarCount(byPlotId.getCarCount());
            });
            // 异步统计设备信息
            CompletableFuture<Void> voidCompletableFuture1 = CompletableFuture.runAsync(() -> {
                PlotVO byPlotId = plotMapper.findEquipByPlotId(plotVO.getId());
                plotVO.setVehicleCount(byPlotId.getVehicleCount());
                plotVO.setFaceCount(byPlotId.getFaceCount());
                plotVO.setVideoCount(byPlotId.getVideoCount());
                plotVO.setDoorCount(byPlotId.getDoorCount());
                plotVO.setEquipmentCount(byPlotId.getEquipmentCount());
            });
            // 异步统计房屋信息
            CompletableFuture<Void> voidCompletableFuture2 = CompletableFuture.runAsync(() -> {
                PlotVO byPlotId = plotMapper.findTeneByPlotId(plotVO.getId());
                plotVO.setIdleCount(byPlotId.getIdleCount());
                plotVO.setHouseCount(byPlotId.getHouseCount());
                plotVO.setLeaseCount(byPlotId.getLeaseCount());
                plotVO.setOtherCount(byPlotId.getOtherCount());
                plotVO.setBuildingCount(byPlotId.getBuildingCount());
                plotVO.setRoomCount(byPlotId.getRoomCount());
            });
            // 等待所有异步任务完成
            CompletableFuture.allOf(voidCompletableFuture, voidCompletableFuture1, voidCompletableFuture2).join();
        }
        // 将查询结果转换为分页信息对象
        PageInfo<PlotVO> plotVOPageInfo = new PageInfo<>(all);
        return plotVOPageInfo;
    }

    @Override
    public List<PlotPO> findPlotAll() {
        List<PlotPO> all = plotMapper.findPlotAll();
        return all;
    }

    @Override
    public List<PlotVO> findplot(Integer id) {
        return plotMapper.findplot(id);
    }

    @Override
    public List<AllCountVO> findAllCount() {
        return plotMapper.findAllCount();
    }

    @Override
    public PopulationCount findPopulationCount() {
        return plotMapper.findPopulationCount();
    }

    @Override
    public HouseTypeVO findHouseTypeCount() {
        return plotMapper.findHouseTypeCount();
    }

    @Override
    public List<PlotVO> findPlotLists() {
        return plotMapper.findPlotLists();
    }

    @Override
    public PlotVO findByPlotId(Long id) {
        PlotVO all = plotMapper.findByPlotId(id);
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            PlotVO byPlotId = plotMapper.findCarByPlotIdCar(id);
            all.setPopulationCount(byPlotId.getPopulationCount());
            all.setForeignCount(byPlotId.getForeignCount());
            all.setForeignerCount(byPlotId.getForeignerCount());
            all.setRegisterCount(byPlotId.getRegisterCount());
            all.setCarCount(byPlotId.getCarCount());
        });
        //统计设备
        CompletableFuture<Void> voidCompletableFuture1 = CompletableFuture.runAsync(() -> {
            PlotVO byPlotId = plotMapper.findEquipByPlotId(id);
            all.setVehicleCount(byPlotId.getVehicleCount());
            all.setFaceCount(byPlotId.getFaceCount());
            all.setVideoCount(byPlotId.getVideoCount());
            all.setDoorCount(byPlotId.getDoorCount());
            all.setEquipmentCount(byPlotId.getEquipmentCount());
        });
        //统计房屋
        CompletableFuture<Void> voidCompletableFuture2 = CompletableFuture.runAsync(() -> {
            PlotVO byPlotId = plotMapper.findTeneByPlotId(id);
            all.setIdleCount(byPlotId.getIdleCount());
            all.setHouseCount(byPlotId.getHouseCount());
            all.setLeaseCount(byPlotId.getLeaseCount());
            all.setOtherCount(byPlotId.getOtherCount());
            all.setBuildingCount(byPlotId.getBuildingCount());
            all.setRoomCount(byPlotId.getRoomCount());
        });
        CompletableFuture.allOf(voidCompletableFuture, voidCompletableFuture1, voidCompletableFuture2).join();
        return all;
    }

    /**
     * 根据小区id查询
     * @param id
     * @return
     */
    @Override
    public List<PlotPO> findByDivisionId(Long id) {
        List<PlotPO> byDivisionId = plotMapper.findByDivisionId(id);
        return byDivisionId;
    }

    @Override
    public String findOutline(Integer id) {
        return plotMapper.findOutline(id);
    }

    @Override
    public List<String> findAllOutLine() {
        return plotMapper.findAllOutLine();
    }

    @Override
    public void add(PlotPO plotPO) {
        if (plotPO.getOutline() != null && !plotPO.getOutline().isEmpty()) {
            plotPO.setOutline(Collections.singletonList(String.join(",", plotPO.getOutline())));
        } else {
            // 如果为空，你可能想设置为null或空字符串，取决于你的数据库和业务逻辑
            plotPO.setOutline(null); // 或 ""
        }

        plotMapper.add(convertToEntity(plotPO));
    }

    private PlotVO convertToEntity(PlotPO plotPO) {
        PlotVO plotEntity = new PlotVO();

        // 复制基本属性
        plotEntity.setId(plotPO.getId());
        plotEntity.setName(plotPO.getName());
        plotEntity.setAddress(plotPO.getAddress());
        plotEntity.setPic(plotPO.getPic());
        plotEntity.setNum(plotPO.getNum());
        plotEntity.setDivisionId(Long.valueOf(plotPO.getDivisionId()));
        plotEntity.setLongitude(plotPO.getLongitude());
        plotEntity.setLatitude(plotPO.getLatitude());
        plotEntity.setEntrance(plotPO.getEntrance());
        plotEntity.setExit(plotPO.getExit());

        // 处理outline属性，将其从List<String>转换为逗号分隔的String
        if (plotPO.getOutline() != null && !plotPO.getOutline().isEmpty()) {
            plotEntity.setOutline((String) plotPO.getOutline().stream().collect(Collectors.joining(",")));
        } else {
            plotEntity.setOutline(null); // 或者设置为空字符串""，取决于你的业务需求
        }

        return plotEntity;
    }

    @Override
    public List<PlotPO> select() {
        List<PlotPO> select = plotMapper.select();
        return select;
    }

//    public static void main(String[] args){ // 测试数据点
//        String sampleText1 ="李骏强19512425283";
//        String sampleText2 ="李聪-18668630875";
//        String sampleText3 ="包乐天+13513860573";
//
//        String regex = "(?<=\\D|^)1\\d{10}(?=\\D|$)";
//
//        System.out.println(extractPhoneNumber(sampleText1, regex));
//        System.out.println(extractPhoneNumber(sampleText2, regex));
//        System.out.println(extractPhoneNumber(sampleText3, regex));
//    }

    private static String extractPhoneNumber(String text, String regex) {
        // 使用正则表达式匹配手机号
        java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(regex);
        java.util.regex.Matcher matcher = pattern.matcher(text);

        // 如果找到匹配项，则返回它，否则返回空字符串
        if (matcher.find()) {
            return matcher.group();
        }
        return "";
    }

    public static void main(String[] args){ // 测试数据点
        String sampleText1 = "李骏强19512425283";
        String sampleText2 = "李聪-18668630875";
        String sampleText3 = "包乐天1+123456789101";

        String regex = "1[3-9]\\d{9}$";

        System.out.println("sampleText1："+extractPhoneNumber(sampleText1, regex));
        System.out.println("sampleText2："+extractPhoneNumber(sampleText2, regex));
        System.out.println("sampleText3："+extractPhoneNumber(sampleText3, regex));
    }

}
